package com.wlyuan.index.remote.facade;

import com.alibaba.fastjson.JSON;
import com.wlyuan.core.domain.valueobj.CombineId;
import com.wlyuan.core.domain.valueobj.WaybillId;
import com.wlyuan.core.exception.AssertUtils;
import com.wlyuan.index.api.dto.QuotationIndexDTO;
import com.wlyuan.index.api.dto.combine.CombineIndexDTO;
import com.wlyuan.index.api.dto.waybill.WaybillIndexDTO;
import com.wlyuan.index.domain.source.adapter.QuotationAdapter;
import com.wlyuan.index.infrastructure.exception.QuotationNoFound;
import com.wlyuan.quotation.api.IQuotationService;
import com.wlyuan.quotation.vo.quotation.QuotationDetailVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yuanjie
 * <p>
 * 运单防腐层
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class QuotationFacade {
    @DubboReference(check = false)
    private IQuotationService quotationService;

    public QuotationIndexDTO getQuotationById(Long quotationId) {
        var quotation = quotationService.getQuotationById(quotationId);
        AssertUtils.assertNotNull(quotation, new QuotationNoFound(), quotationId);
        if (logger.isDebugEnabled()) {
            logger.debug("Quotation: {}", JSON.toJSON(quotation));
        }
        return QuotationAdapter.translate(quotation);
    }

    public List<QuotationIndexDTO> listQuotations(CombineIndexDTO combine) {
        var quotations = quotationService.listQuotations(combine.getCombineId(), QuotationIndexDTO.COMBINE);
        if (CollectionUtils.isEmpty(quotations)) {
            return Collections.emptyList();
        }
        return quotations.stream()
                .map(QuotationAdapter::translate)
                .collect(Collectors.toList());
    }

    public List<QuotationIndexDTO> listQuotations(WaybillIndexDTO waybill) {
        List<QuotationDetailVO> quotations;
        if (null != waybill.getCombineId()) {
            quotations = quotationService.listQuotations(waybill.getCombineId(), QuotationIndexDTO.COMBINE);
        } else {
            quotations = quotationService.listQuotations(waybill.getWaybillId(), QuotationIndexDTO.WAYBILL);
        }

        if (CollectionUtils.isEmpty(quotations)) {
            return Collections.emptyList();
        }
        return quotations.stream()
                .map(QuotationAdapter::translate)
                .collect(Collectors.toList());
    }

    public List<QuotationIndexDTO> listQuotations(WaybillId waybillId) {
        try {
            var quotations = quotationService.listQuotations(waybillId.getId(), QuotationIndexDTO.WAYBILL);
            if (CollectionUtils.isEmpty(quotations)) {
                return Collections.emptyList();
            }
            return quotations.stream()
                    .map(QuotationAdapter::translate)
                    .collect(Collectors.toList());
        } catch (Exception e) {
            logger.error("查询报价信息失败: {}", waybillId, e);
        }

        return Collections.emptyList();
    }

    public List<QuotationIndexDTO> listQuotations(CombineId combineId) {
        var quotations = quotationService.listQuotations(combineId.getId(), QuotationIndexDTO.COMBINE);
        if (CollectionUtils.isEmpty(quotations)) {
            return Collections.emptyList();
        }
        return quotations.stream()
                .map(QuotationAdapter::translate)
                .collect(Collectors.toList());
    }
}

